package com.bugsnag.ohos;

import ohos.app.Context;
import ohos.app.ElementsCallback;
import ohos.global.configuration.Configuration;

/**
 * ClientComponentCallbacks
 *
 * @since 2021-07-20
 */
public class ClientComponentCallbacks implements ElementsCallback {
    private Context context;
    private DeviceDataCollector deviceDataCollector;
    private Function1<Boolean> function1;
    private Function2<String, String> function2;

    /**
     * constructor
     *
     * @param context
     * @param deviceDataCollector
     * @param function1
     * @param function2
     */
    public ClientComponentCallbacks(Context context, DeviceDataCollector deviceDataCollector,
                                    Function1<Boolean> function1, Function2<String, String> function2) {
        this.context = context;
        this.deviceDataCollector = deviceDataCollector;
        this.function1 = function1;
        this.function2 = function2;
    }

    @Override
    public void onMemoryLevel(int i) {
        if (function1 != null) {
            function1.invoke(true);
        }
    }

    @Override
    public void onConfigurationUpdated(Configuration configuration) {
        String oldOrientation = deviceDataCollector.getOrientationAsString();
        if (context != null && context.getAbilityInfo() != null) {
            if (deviceDataCollector.updateOrientation(context.getAbilityInfo().getOrientation())) {
                String newOrientation = deviceDataCollector.getOrientationAsString();
                if (function2 != null) {
                    function2.invoke(oldOrientation, newOrientation);
                }
            }
        }
    }

    /**
     * Function1
     *
     * @param <T>
     * @since 2021-07-26
     */
    interface Function1<T> {
        /**
         * invoke
         *
         * @param t
         */
        void invoke(T t);
    }

    /**
     * Function2
     *
     * @param <T1>
     * @param <T2>
     * @since 2021-07-26
     */
    interface Function2<T1, T2> {
        /**
         * invoke
         *
         * @param t1
         * @param t2
         */
        void invoke(T1 t1, T2 t2);
    }
}
